import Vue from 'vue';
import Router from 'vue-router';
import Cookie from "js-cookie"

import Home from "../views/Home";
import Employee from "../views/Employee";
import Admin from "../views/Admin";
import User from "../views/User";
import Main from "../views/Main";
import Dept from "../views/Dept";
import Role from "../views/Role";
import Menu from "../views/Menu";
import Login from "../views/Login"
import SystemAddress from "../views/SystemAddress";
import FileAddress from "../views/FileAddress";
import UploadFile from "../views/UploadFile";
import Advertisement from "../views/Advertisement";

Vue.use(Router);

//1.创建路由组件
//2.将路由与租组件映射
//3.创建router实力

const routes=[
  {
    path:"/",
    component:Main,
    name: "main",
    redirect: "/home",  //重定向
    children:[
      {
        path:'/home',
        name: 'home',
        component:Home
      },{
        path: '/system/user',
        name: 'user',
        component: User
      },{
        path: '/system/employee',
        name: 'employee',
        component: Employee
      },{
        path: '/system/admin',
        name: 'admin',
        component: Admin
      },{
        path: '/system/user',
        name: 'user',
        component: User
      },{
        path: '/system/role',
        name: 'role',
        component: Role
      },{
        path: '/system/dept',
        name: 'dept',
        component: Dept
      },{
        path: '/system/menu',
        name: 'menu',
        component: Menu
      },{
        path: '/system/advertisement',
        name: 'advertisement',
        component: Advertisement
      },{
        path: '/file/sysAddress',
        name: 'systemAddress',
        component: SystemAddress
      },{
        path: '/file/fileAddress',
        name: 'fileAddress',
        component: FileAddress
      },{
        path: '/file/uploadFile',
        name: 'uploadFile',
        component: UploadFile
      }
    ]
  },{
    path: '/login',
    name: 'login',
    component: Login
  }
];
const router=new Router({
  routes//缩写 相当于routes:routes
});

//添加全局前置导航守卫
router.beforeEach((to,from,next)=>{
  //判断token是否存在
  const token = Cookie.get("token");
  if (!token && to.name!="login"){
    //不存在，且不在登录页面
    next({name:'login'});
  }else if (token && to.name=="login"){
    //存在，且在登录页面
    next({name:from.name});
  }else{
    next();
  }
});


export default router;

/*export default new Router({
routes:[
  {
    path:'/home',
    component:Home
  },{
    path: '/user',
    component: User
  }
]
});*/














